package com.ruoyi.web.controller.nongjt;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.nongjt.domain.NjtUserRegister;
import com.ruoyi.nongjt.domain.dto.NjtUserRegisterDto;
import com.ruoyi.nongjt.service.NjtUserRegisterService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.ruoyi.framework.datasource.DynamicDataSourceContextHolder.log;

@RestController
@RequestMapping("/nongjt/userRegister")
public class NjtUserRegisterController extends BaseController {
    @Autowired
    private NjtUserRegisterService njtUserRegisterService;

    //    有条件的分页查询
    @GetMapping("/list")
    public TableDataInfo list(NjtUserRegister njtUserRegister) {
        startPage();
        List<NjtUserRegister> list = njtUserRegisterService.getNjtUserRegisterList(njtUserRegister);
        return getDataTable(list);
    }

    @PutMapping("/update")
    public AjaxResult update(@RequestBody NjtUserRegister njtUserRegister) {
        int rows = njtUserRegisterService.updateNjtUserRegister(njtUserRegister);
//        if (rows > 0) {
//            return AjaxResult.success("修改成功");
//        } else {
//            return AjaxResult.error("修改失败");
//        }
//        若依官方封装一个方法用户判断受影响行数，可以直接继承BaseController，然后调用
        return toAjax(rows);
    }

    @DeleteMapping("/delete")
    public AjaxResult deleteBatch(@RequestBody NjtUserRegister batchDeleteDTO) {
        log.info("接收到删除请求，参数: {}", batchDeleteDTO);

        if (batchDeleteDTO == null || batchDeleteDTO.getIds() == null || batchDeleteDTO.getIds().isEmpty()) {
            return AjaxResult.error("删除失败，未提供有效的ID列表");
        }

        log.info("准备删除的ID列表: {}", batchDeleteDTO.getIds());
        int affectedRows = njtUserRegisterService.deleteNjtUserRegisterByIds(batchDeleteDTO.getIds());
        log.info("删除操作影响行数: {}", affectedRows);

        if (affectedRows > 0) {
            return AjaxResult.success("删除成功，共删除 " + affectedRows + " 条记录");
        }
        return AjaxResult.error("删除失败，未找到对应记录或删除过程出错");
    }


    @PostMapping("/export")
    public void export(HttpServletResponse response, NjtUserRegister njtUserRegister) {
        List<NjtUserRegister> list = njtUserRegisterService.getNjtUserRegisterList(njtUserRegister);
        ExcelUtil<NjtUserRegister> util = new ExcelUtil<>(NjtUserRegister.class);
        util.exportExcel(response, list, "用户注册数据");
    }
    @PostMapping("/register")
    public AjaxResult register(@Valid @RequestBody NjtUserRegister user) {
        NjtUserRegister query = new NjtUserRegister();
        query.setMobilePhone(user.getMobilePhone());
        List<NjtUserRegister> list = njtUserRegisterService.getNjtUserRegisterList(query);
        if (!list.isEmpty()) {
            return AjaxResult.error("该手机号已经注册!!!");
        }
        // 设置默认值
        user.setRegisterTime(new Date());
        user.setDelFlag("0"); // 没删除
        user.setUserStatus("0"); // 正常的状态
        user.setPerfectFlag("0"); // 没有完善信息
        user.setMemberFlag("0"); // 不是会员
        int rows = njtUserRegisterService.insertNjtUserRegister(user);
        return toAjax(rows);
    }
    @PostMapping("/login")
    public AjaxResult login(@RequestBody Map<String, String> params) {
        String mobile = params.get("mobilePhone");
        String password = params.get("password");
        NjtUserRegister query = new NjtUserRegister();
        query.setMobilePhone(mobile);
        List<NjtUserRegister> list = njtUserRegisterService.getNjtUserRegisterList(query);
        if (list.isEmpty()) {
            return AjaxResult.error("用户不存在呢");
        }
        NjtUserRegister user = list.get(0);
        if (!password.equals(user.getPassword())) {
            return AjaxResult.error("密码错误！！！");
        }
        if ("1".equals(user.getUserStatus())) {
            return AjaxResult.error("账号已禁用！！！");
        }
        return AjaxResult.success("登录成功！！！", user);
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody NjtUserRegister njtUserRegister) {
        int result = njtUserRegisterService.insertNjtUserRegister(njtUserRegister);
        return toAjax(result);
    }




}

